package com.kele.utils;

import com.kele.ddl.CreateTable;
import com.kele.entity.DbModel;

import java.sql.*;
import java.util.*;

public class DbDetailsUtils {

    static ResultSet resultSet;
    static Statement stmt;
    static Connection conn;
    static ResultSetMetaData metaData;

    public static List execute(String sql) throws SQLException {

        conn = CreateTable.getConnection();

        stmt = conn.createStatement();

        resultSet=stmt.executeQuery(sql);

        metaData=resultSet.getMetaData();
        int columnCount=metaData.getColumnCount();

        List<String> list = new ArrayList<>();
        Map<String,String> map=new HashMap<>(16);
        while (resultSet.next()){
            for (int i = 1; i <=columnCount ; i++) {
                map.put(metaData.getColumnName(i), resultSet.getString(i));

                //list.add(map);
            }
            //System.out.println(map);
            //System.out.println(list);
//            for(Object s:map.keySet()){
//                System.out.println("key : "+s+" value : "+map.get(s));
//               list.add(map);
//            }
            for (Map.Entry<String, String> entry:map.entrySet()){
                list.add(entry.getKey()+" : "+entry.getValue());
            }
        }
        return list;

    }
}
